package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Set;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;
    @Autowired
    private MenuDao menuDao;
    @Override
    public Set<Role> findAll() {
        Set<Role> allRoles = roleDao.findAllRoles();
        return allRoles;
    }
//条件枫叶查询角色
    @Override
    public PageResult listByPage(Integer page, Integer limit, String query) {

        //分页
        PageHelper.startPage(page,limit);
        Page<Role> pageList=roleDao.findByQuery(query);

        //获取总页数
        long total = pageList.getTotal();
        //获取数据
        List<Role> result = pageList.getResult();
        //添加到返回中
       PageResult pageResult=new PageResult(total,result);
        return pageResult;
    }
    //根据角色id查询当前角色的权限
    @Override
    public Set<Permission> findPermissionByRoleId(Integer id) {
        Set<Permission> list = permissionDao.findByRoleId(id);
        return list;
    }
    //修改角色权限
    @Override
    public void savePermissionByRoleId(Integer roleId,Integer[] checkPermission) {
        //删除角色权限中间表关系
            roleDao.deletePermissionandRoleId(roleId);
        //建立新的角色权限关系
        for (Integer permissionId : checkPermission) {
            roleDao.createNewPermissionAndRole(roleId,permissionId);
        }

    }
//添加角色
    @Override
    public void addRole(Role role) {
        roleDao.addRole(role);
    }
    //修改角色
    @Override
    public void editRole(Role role) {
        roleDao.editRole(role);
    }
//查询角色byid
    @Override
    public Role findRoleById(Integer id) {
        Role role=roleDao.findRoleById(id);
        return role;
    }
    //保存菜单by角色id
    @Override
    public void editMenuByRoleId(Integer id, Integer[] menuIds) {
        //根据角色id删除中间表的菜单id
        roleDao.deleteMenuIdsByRoleId(id);
        //添加修改的菜单id 通过角色id
        for (Integer menuId : menuIds) {
            roleDao.editMenuIdsByRoleId(id,menuId);
        }

    }
    //查询当前角色拥有的菜单id
    @Override
    public Set<Integer> findMenusByRoleId(Integer id) {
        Set<Integer> menusByRoleId = menuDao.findMenusByRoleId(id);
        return menusByRoleId;
    }
    //删除角色by角色id
    @Override
    public void deleteByRoleId(Integer id) {
        //删除角色和菜单的中间表
        roleDao.deleteMenuIdsByRoleId(id);
        //删除角色和权限的中间表
        roleDao.deletePermissionandRoleId(id);
        //删除角色和用户的中间表
        roleDao.deleteUserByRoleId(id);
        //删除角色
        roleDao.deleteRoleByRoleId(id);
    }
    //删除角色by角色ids
    @Override
    public void deleteRoleByRoleIds(Integer[] ids) {
        //遍历ids
        for (Integer id : ids) {
            this.deleteByRoleId(id);
        }
    }
}
